package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Basket;
import com.powernode.model.Result;
import com.powernode.service.BasketService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.CartTotalAmount;
import com.powernode.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 购物车业务控制层
 */
@Api(tags = "购物车接口管理")
@RequestMapping("p/shopCart")
@RestController
public class BasketController {

    @Autowired
    private BasketService basketService;


    @ApiOperation("查询会员购物车中商品的数量")
    @GetMapping("prodCount")
    public Result<Integer> loadMemberBasketProdCount() {
        String openId = AuthUtils.getLoginUserOpenId();
        Integer basketCount = basketService.queryMemberBasketProdCount(openId);
        return Result.success(basketCount);
    }

//    p/shopCart/info

    /**
     * 该方法是支持多店铺展示数据
     *
     * @return
     */
    @ApiOperation("查询会员购物车列表")
    @GetMapping("info")
    public Result<CartVo> loadMemberCartVo() {
        String openId = AuthUtils.getLoginUserOpenId();
        CartVo cartVo = basketService.queryMemberCartVo(openId);
        return Result.success(cartVo);
    }

//    p/shopCart/totalPay

    /**
     * 计算会员选中商品的金额
     * @param basketIdList 购物车id集合
     * @return CartTotalAmount 购物车商品总金额对象
     */
    @ApiOperation("计算会员选中商品的金额")
    @PostMapping("totalPay")
    public Result<CartTotalAmount> loadMemberCheckedCartTotalAmount(@RequestBody List<Long> basketIdList) {
        CartTotalAmount cartTotalAmount = basketService.queryMemberCheckedCartTotalAmount(basketIdList);
        return Result.success(cartTotalAmount);
    }


    /**
     * 添加商品到购物车或修改购物车中商品数量
     * @return
     */
//    p/shopCart/changeItem
    @ApiOperation("添加商品到购物车或修改购物车中商品数量")
    @PostMapping("changeItem")
    public Result<String> changeCartItem(@RequestBody Basket basket) {
        String openId = AuthUtils.getLoginUserOpenId();
        basket.setOpenId(openId);
        Integer count = basketService.changeCartItem(basket);
        return Result.handle(count>0);
    }

    /**
     * 删除会员选中的购物车记录
     * @param basketIds 购物车id集合
     * @return
     */
//    p/shopCart/deleteItem
    @ApiOperation("删除会员选中的购物车记录")
    @DeleteMapping("deleteItem")
    public Result<String> removeMemberCheckedCartItem(@RequestBody List<Long> basketIds) {
        boolean removed = basketService.removeBatchByIds(basketIds);
        return Result.handle(removed);
    }

    /////////////////////////////// feign 接口 ////////////////////////////////////
    @ApiOperation("根据购物车id集合查询购物车记录")
    @GetMapping("getBasketListByIds")
    public Result<List<Basket>> getBasketListByIds(@RequestParam List<Long> ids) {
        List<Basket> basketList = basketService.listByIds(ids);
        return Result.success(basketList);
    }

    @DeleteMapping("removeBasketByOpenIdAndSkuIds")
    public Result<Boolean> removeBasketByOpenIdAndSkuIds(@RequestBody Map<String,Object> params) {
        String openId = (String) params.get("openId");
        List<Long> skuIdList = (List<Long>) params.get("skuIdList");
        boolean removed = basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getOpenId, openId)
                .in(Basket::getSkuId, skuIdList)
        );
        return Result.success(removed);
    }
}
